﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace PermissionCommon
{
    public class CookiesHelper
    {
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void SetCookie(string key,string value)
        {
            SetCookie(key, null, value, null, null);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires">到期时间</param>
        /// <param name="Domain">域名</param>
        public static void SetCookie(string cookieName,string key,string value,DateTime? expires,string Domain)
        {
            HttpResponse response = HttpContext.Current.Response;
            if (response != null)
            {
                HttpCookie cookie = response.Cookies[cookieName];
                if (cookie != null)
                {
                    cookie.Path = "/";
                    if (!string.IsNullOrEmpty(Domain))
                    {
                        cookie.Domain = Domain;
                    }
                    if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
                    {
                        cookie.Values.Set(key, value);
                    }
                    else if (!string.IsNullOrEmpty(value))
                    {
                        cookie.Value = value;
                    }
                    if (expires.HasValue)
                    {
                        cookie.Expires = expires.Value;
                    }
                    response.SetCookie(cookie);
                }
            }
        }
   
        /// <summary>
        /// 获取Cookie值
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public static string GetCookieValue(string cookieName)
        {
            return GetCookieValue(cookieName,null);
        }
        /// <summary>
        /// 获取Cookie值
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        private static string GetCookieValue(string cookieName, string key)
        {
            HttpRequest request = HttpContext.Current.Request;
            if(request == null)
            {
                return "";
            }
            if (request.Cookies[cookieName] != null)
            {
                if (!string.IsNullOrEmpty(key) && request.Cookies[cookieName].HasKeys)
                {
                    return request.Cookies[cookieName].Values[key];
                }
                else
                {
                    return request.Cookies[cookieName].Value;
                }   
            }
            string str = ", " + cookieName;
            if (request.Cookies[str] == null)
            {
                return "";
            }
            if (!string.IsNullOrEmpty(key) && request.Cookies[str].HasKeys)
            {
                return request.Cookies[str].Values[key];
            }
            return request.Cookies[str].Value;
        }

        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="expires">有效期</param>
        public static void AddCookie(string cookieName, DateTime expires)
        {
            HttpCookie cookie = new HttpCookie(cookieName)
            {
                Expires = expires
            };
            AddCookie(cookie, null);
        }

        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="cookie"></param>
        /// <param name="Domain">域名</param>
        public static void AddCookie(HttpCookie cookie, string Domain)
        {
            HttpResponse response = HttpContext.Current.Response;
            if (response != null)
            {
                cookie.HttpOnly = true;
                cookie.Path = "/";
                if (!string.IsNullOrEmpty(Domain))
                {
                    cookie.Domain = Domain;
                }
                response.AppendCookie(cookie);
            }
        }
    }
}
